package zzs.generalframework.console.core.authorization.authuserrolemap.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.authorization.authuserrolemap.AuthUserRoleMapInfo;
import zzs.generalframework.common.core.authorization.authuserrolemap.AuthUserRoleMapPO;
import zzs.generalframework.console.core.authorization.authuserrolemap.dao.IAuthUserRoleMapDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.authorization.authuserrolemap.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.authorization.authuserrolemap.dao.impl.authUserRoleMapDaoImpl")
public class AuthUserRoleMapDaoImpl implements IAuthUserRoleMapDao
{

	private Logger logger = LoggerFactory.getLogger(AuthUserRoleMapDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(AuthUserRoleMapPO authUserRoleMapPO)
	{
		logger.info("====插入权限用户角色映射数据开始====");
		logger.debug("====插入权限用户角色映射数据,入参:AuthUserRoleMapPO=[{}]====", authUserRoleMapPO);
		entityManager.persist(authUserRoleMapPO);
		logger.debug("====插入权限用户角色映射数据,出参:boolean=[{}]====", true);
		logger.info("====插入权限用户角色映射数据结束====");
		return true;
	}

	@Override
	public boolean update(AuthUserRoleMapPO authUserRoleMapPO)
	{
		logger.info("====更新权限用户角色映射数据开始====");
		logger.debug("====更新权限用户角色映射数据,入参:AuthUserRoleMapPO=[{}]====", authUserRoleMapPO);
		entityManager.merge(authUserRoleMapPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新权限用户角色映射数据结束====");
		return true;
	}

	@Override
	public List<AuthUserRoleMapPO> select(AuthUserRoleMapInfo authUserRoleMapInfo)
	{
		logger.info("====查询权限用户角色映射数据开始====");
		logger.debug("====查询权限用户角色映射数据,入参:AuthUserRoleMapInfo=[{}]====", authUserRoleMapInfo);
		List<AuthUserRoleMapPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from AuthUserRoleMapPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(authUserRoleMapInfo.getUserRoleMapID()))
		{
			sqlBuffer.append(" and a.userRoleMapID=?");
			parameter.putParameterObject(authUserRoleMapInfo.getUserRoleMapID());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapInfo.getUserCode()))
		{
			sqlBuffer.append(" and a.userCode=?");
			parameter.putParameterObject(authUserRoleMapInfo.getUserCode());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapInfo.getRoleCode()))
		{
			sqlBuffer.append(" and a.roleCode=?");
			parameter.putParameterObject(authUserRoleMapInfo.getRoleCode());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapInfo.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(authUserRoleMapInfo.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapInfo.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(authUserRoleMapInfo.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(authUserRoleMapInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询权限用户角色映射数据,出参:List<AuthUserRoleMapPO>=[{}]====", returnList);
		logger.info("====查询权限用户角色映射数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(AuthUserRoleMapPO authUserRoleMapPO)
	{
		logger.info("====删除权限用户角色映射数据开始====");
		logger.debug("====删除权限用户角色映射数据,入参:AuthUserRoleMapPO=[{}]====", authUserRoleMapPO);
		entityManager.remove(authUserRoleMapPO);
		logger.debug("====删除权限用户角色映射数据,出参:boolean=[{}]====", true);
		logger.info("====删除权限用户角色映射数据结束====");
		return true;
	}

	@Override
	public Long count(AuthUserRoleMapPO authUserRoleMapPO)
	{
		logger.info("====统计权限用户角色映射数据开始====");
		logger.debug("====统计权限用户角色映射数据,入参:AuthUserRoleMapPO=[{}]====", authUserRoleMapPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from AuthUserRoleMapPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(authUserRoleMapPO.getUserRoleMapID()))
		{
			sqlBuffer.append(" and a.userRoleMapID=?");
			parameter.putParameterObject(authUserRoleMapPO.getUserRoleMapID());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapPO.getUserCode()))
		{
			sqlBuffer.append(" and a.userCode=?");
			parameter.putParameterObject(authUserRoleMapPO.getUserCode());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapPO.getRoleCode()))
		{
			sqlBuffer.append(" and a.roleCode=?");
			parameter.putParameterObject(authUserRoleMapPO.getRoleCode());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapPO.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(authUserRoleMapPO.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapPO.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(authUserRoleMapPO.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(authUserRoleMapPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(authUserRoleMapPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计权限用户角色映射数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计权限用户角色映射数据结束====");
		return returnLong;
	}

}
